package com.consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.constant.AuthConstant;
import com.constant.DBTableNameConstant;
import com.constant.TopicConstant;
import com.domain.dto.CanalBean;
import com.domain.dto.TokenRedisDTO;
import com.domain.entity.*;
import com.utils.RedisClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import com.pool.TreadPoolUtils;

import java.util.List;


@Component
@Slf4j
public class TokenConsumer {

    @Autowired
    private RedisClient redisClient;

    @KafkaListener(topics = TopicConstant.LISTEN_DB_TOPIC)
    public void receive(ConsumerRecord<?, ?> consumer) {
        TreadPoolUtils.getTokenConsumerPool().execute(() -> {

            String value = (String) consumer.value();
            log.info("topic名称:{},key:{},分区位置:{},下标:{},value:{}", consumer.topic(), consumer.key(), consumer.partition(), consumer.offset(), value);
            //转换为javaBean
            JSONObject jsonObject = JSONObject.parseObject(value);

            //首先判断操作的是哪张表
            String tableName = jsonObject.getString("table");
            if (DBTableNameConstant.DB_TOKEN_TABLE.equals(tableName)) {

                //获取是否是DDL语句
                boolean isDdl = jsonObject.getBoolean("isDdl");
                //获取类型
                String type = jsonObject.getString("type");
                if (!isDdl) {
                    CanalBean<TokenRedisDTO> dtoCanalBean = JSONObject.parseObject(value, new TypeReference<CanalBean<TokenRedisDTO>>() {
                    });

                    List<TokenRedisDTO> tokenList = dtoCanalBean.getData();

                    long TIME_OUT = 24L;
                    if (DBTableNameConstant.DB_INSERT.equals(type) || DBTableNameConstant.DB_UPDATE.equals(type)) {
                        //新增语句
                        for (TokenRedisDTO t : tokenList) {
                            String token = t.getToken();
                            redisClient.setString(AuthConstant.tokenPrefix + token, JSON.toJSONString(t), TIME_OUT);
                        }
                    } else if (DBTableNameConstant.DB_DELETE.equals(type)) {
                        //删除语句
                        for (TokenRedisDTO t : tokenList) {
                            String token = t.getToken();
                            redisClient.deleteKey(AuthConstant.tokenPrefix + token);
                        }
                    }
                }
            } else if (DBTableNameConstant.DB_MAINTENANCEMAN_TABLE.equals(tableName)) {

                //获取是否是DDL语句
                boolean isDdl = jsonObject.getBoolean("isDdl");
                //获取类型
                String type = jsonObject.getString("type");
                if (!isDdl) {

                    CanalBean<WeixiuyuanEntity> dtoCanalBean = JSONObject.parseObject(value, new TypeReference<CanalBean<WeixiuyuanEntity>>() {
                    });

                    List<WeixiuyuanEntity> dataList = dtoCanalBean.getData();

                    if (!DBTableNameConstant.DB_SELECT.equals(type)) {
                        //到redis中删除对应的缓存
                        for (WeixiuyuanEntity e : dataList) {

                            Long id = e.getId();
                            redisClient.deleteKey(AuthConstant.MAINTENANCEMAN_INFO_KEY + id);
                        }

                    }
                }

            }else if (DBTableNameConstant.DB_USER_TABLE.equals(tableName)) {

                //获取是否是DDL语句
                boolean isDdl = jsonObject.getBoolean("isDdl");
                //获取类型
                String type = jsonObject.getString("type");
                if (!isDdl) {

                    CanalBean<UserEntity> dtoCanalBean = JSONObject.parseObject(value, new TypeReference<CanalBean<UserEntity>>() {
                    });

                    List<UserEntity> dataList = dtoCanalBean.getData();

                    if (!DBTableNameConstant.DB_SELECT.equals(type)) {
                        //到redis中删除对应的缓存
                        for (UserEntity e : dataList) {

                            Long id = e.getId();
                            redisClient.deleteKey(AuthConstant.ADMIN_INFO_KEY + id);
                        }

                    }
                }
            } else if (DBTableNameConstant.DB_RESTORE_TABLE.equals(tableName)) {
                //获取是否是DDL语句
                boolean isDdl = jsonObject.getBoolean("isDdl");
                //获取类型
                String type = jsonObject.getString("type");
                if (!isDdl) {

                    CanalBean<WeixiuhuifuEntity> dtoCanalBean = JSONObject.parseObject(value, new TypeReference<CanalBean<WeixiuhuifuEntity>>() {
                    });

                    List<WeixiuhuifuEntity> dataList = dtoCanalBean.getData();

                    if (!DBTableNameConstant.DB_SELECT.equals(type)) {
                        //到redis中删除对应的缓存
                        for (WeixiuhuifuEntity e : dataList) {

                            Long id = e.getId();
                            redisClient.deleteKey(AuthConstant.RESTORED_INFO_PREFIX + id);
                        }

                    }
                }

            }else if (DBTableNameConstant.DB_REPORT_TABLE.equals(tableName)) {
                //获取是否是DDL语句
                boolean isDdl = jsonObject.getBoolean("isDdl");
                //获取类型
                String type = jsonObject.getString("type");
                if (!isDdl) {

                    CanalBean<ChongdianzhuangbaoxiuEntity> dtoCanalBean = JSONObject.parseObject(value, new TypeReference<CanalBean<ChongdianzhuangbaoxiuEntity>>() {
                    });

                    List<ChongdianzhuangbaoxiuEntity> dataList = dtoCanalBean.getData();

                    if (!DBTableNameConstant.DB_SELECT.equals(type)) {
                        //到redis中删除对应的缓存
                        for (ChongdianzhuangbaoxiuEntity e : dataList) {

                            Long id = e.getId();
                            redisClient.deleteKey(AuthConstant.REPORT_INFO_PREFIX + id);
                        }

                    }
                }

            }else if (DBTableNameConstant.DB_TYPE_TABLE.equals(tableName)) {
                //获取是否是DDL语句
                boolean isDdl = jsonObject.getBoolean("isDdl");
                //获取类型
                String type = jsonObject.getString("type");
                if (!isDdl) {

                    CanalBean<DianzhuangleibieEntity> dtoCanalBean = JSONObject.parseObject(value, new TypeReference<CanalBean<DianzhuangleibieEntity>>() {
                    });

                    List<DianzhuangleibieEntity> dataList = dtoCanalBean.getData();

                    if (!DBTableNameConstant.DB_SELECT.equals(type)) {
                        //到redis中删除对应的缓存
                        for (DianzhuangleibieEntity e : dataList) {

                            Long id = e.getId();
                            redisClient.deleteKey(AuthConstant.TYPE_INFO_PREFIX + id);
                        }

                    }
                }

            }else if (DBTableNameConstant.DB_CHARGING_TABLE.equals(tableName)) {
                //获取是否是DDL语句
                boolean isDdl = jsonObject.getBoolean("isDdl");
                //获取类型
                String type = jsonObject.getString("type");
                if (!isDdl) {

                    CanalBean<ChongdianzhuangEntity> dtoCanalBean = JSONObject.parseObject(value, new TypeReference<CanalBean<ChongdianzhuangEntity>>() {
                    });

                    List<ChongdianzhuangEntity> dataList = dtoCanalBean.getData();

                    if (!DBTableNameConstant.DB_SELECT.equals(type)) {
                        //到redis中删除对应的缓存
                        for (ChongdianzhuangEntity e : dataList) {

                            Long id = e.getId();
                            redisClient.deleteKey(AuthConstant.CHARGING_INFO_PREFIX + id);
                        }

                    }
                }

            }else if (DBTableNameConstant.DB_YONGHU_TABLE.equals(tableName)) {
                //获取是否是DDL语句
                boolean isDdl = jsonObject.getBoolean("isDdl");
                //获取类型
                String type = jsonObject.getString("type");
                if (!isDdl) {

                    CanalBean<YonghuEntity> dtoCanalBean = JSONObject.parseObject(value, new TypeReference<CanalBean<YonghuEntity>>() {
                    });

                    List<YonghuEntity> dataList = dtoCanalBean.getData();

                    if (!DBTableNameConstant.DB_SELECT.equals(type)) {
                        //到redis中删除对应的缓存
                        for (YonghuEntity e : dataList) {

                            Long id = e.getId();
                            redisClient.deleteKey(AuthConstant.YONGHU_INFO_PREFIX + id);
                        }

                    }
                }

            }


        });


    }


}
